package com.lagou.rabbitmq.payorder.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ascetic
 * @version 1.0
 * @ClassName RabbitConfig
 * @Description TODO
 * @date 2021-03-13 23:23
 */
@Configuration
public class RabbitConfig {

    // 业务队列
    @Bean
    public Queue queue(){
        Map<String,Object> props = new HashMap<>();
        // 消息的生存时间 10s
        props.put("x-message-ttl",10000);

        // 设置死信交换器
        props.put("x-dead-letter-exchange","ex.timeout.dlx");

        // 设置该队列所关联的死信交换器的routingkey
        props.put("x-dead-letter-routing-key","timeout.dlx");

        Queue queue = new Queue("order",true,false,false,props);

        return queue;
    }

    @Bean
    public Queue queueDlx(){
        Queue queue = new Queue("timeoutOrder.dlx",true,false,false);
        return queue;
    }

    @Bean
    public Exchange exchange(){
        DirectExchange exchange = new DirectExchange("ex.order",true,false,null);
        return exchange;
    }


    @Bean
    public Exchange exchangeDlx(){
        DirectExchange exchange = new DirectExchange("ex.timeout.dlx",true,false,null);
        return exchange;
    }

    // 业务交换器，绑定业务队列
    @Bean
    public Binding binding(){
        return BindingBuilder.bind(queue()).to(exchange()).with("order").noargs();
    }

    @Bean
    public Binding bindingDlx(){
        return BindingBuilder.bind(queueDlx()).to(exchangeDlx()).with("timeout.dlx").noargs();
    }


}